import requests
from bs4 import BeautifulSoup


class Scrawler():
    def __init__(self):
        self.headers={
            "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.1 Safari/605.1.15"
        }

    def getCodes(self,url):
        return requests.get(url,headers=self.headers).text
#获取纯文本
    def getTexts(self,url,selector):
        bs = BeautifulSoup(self.getCodes(url),"html.parser")
        return [item.text for item in bs.select(selector=selector)]
#获取超链接url
    def getUrl(self,url,selector):
        bs = BeautifulSoup(self.getCodes(url),"html.parser")
        return [item.attrs['href'] for item in bs.select(selector=selector)]
#获取图片的src地址
    def getImgUrl(self,url,selector):
        bs = BeautifulSoup(self.getCodes(url),"html.parser")
        return [item.attrs['src'] for item in bs.select(selector=selector)]
#获取图片
    def getImgs(self,url,imgName):
        rs = requests.get(url,headers=self.headers).content
        with open(imgName,'wb') as f:
            f.write(rs)
